﻿
using Xacml.Core.Interfaces;
using System.Collections;

namespace Xacml.Core.Runtime
{
  public class PolicyCombiningAlgorithmPermitOverrides : IPolicyCombiningAlgorithm
  {
    public Decision Evaluate(EvaluationContext context, IMatchEvaluableCollection policies)
    {
      bool flag1 = false;
      bool flag2 = false;
      foreach (IMatchEvaluable matchEvaluable in (CollectionBase) policies)
      {
        Decision decision = matchEvaluable.Evaluate(context);
        context.TraceContextValues();
        if (decision == Decision.Deny)
        {
          flag2 = true;
        }
        else
        {
          if (decision == Decision.Permit)
          {
            context.ProcessingError = false;
            context.IsMissingAttribute = false;
            return Decision.Permit;
          }
          if (decision != Decision.NotApplicable && decision == Decision.Indeterminate)
            flag1 = true;
        }
      }
      if (flag2)
      {
        context.ProcessingError = false;
        context.IsMissingAttribute = false;
        return Decision.Deny;
      }
      return flag1 ? Decision.Indeterminate : Decision.NotApplicable;
    }
  }
}
